﻿using System;
using System.Security;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.UserCode;


namespace SPLinkedInProxy.ProxyCode
{
    public class SPLinkedInProxyOperation : SPProxyOperation
    {
        string txtRequestToken = string.Empty;
        string txtTokenSecret = string.Empty;
        string txtoAuth_token = string.Empty;
        string txtoAuth_verifier = string.Empty;
        string txtAccessToken = string.Empty;
        string txtAccessTokenSecret = string.Empty;
        string txtApiResponse = string.Empty;


        public override object Execute(SPProxyOperationArgs args)
        {
            oAuthLinkedIn _oauth = new oAuthLinkedIn();
            SPLinkedInProxyArgs proxyArgs = null;
            if (args != null)
            {
                try
                {
                    proxyArgs = args as SPLinkedInProxyArgs;

                    if (proxyArgs.txtCommandName == "Authorize")
                    {
                        string authLink = _oauth.AuthorizationLinkGet();
                        proxyArgs.txtRequestToken = _oauth.Token;
                        proxyArgs.txtTokenSecret = _oauth.TokenSecret;
                        proxyArgs.hypAuthToken = authLink;
                        return proxyArgs;
                    }
                    else if (proxyArgs.txtCommandName == "GetAccessToken")
                    {
                        _oauth.Token = proxyArgs.txtoAuth_token;
                        _oauth.TokenSecret = proxyArgs.txtTokenSecret;
                        _oauth.Verifier = proxyArgs.txtoAuth_verifier;

                        _oauth.AccessTokenGet(proxyArgs.txtoAuth_token.ToString());
                        proxyArgs.txtAccessToken = _oauth.Token;
                        proxyArgs.txtAccessTokenSecret = _oauth.TokenSecret;
                        return proxyArgs;
                    }
                    else if (proxyArgs.txtCommandName == "SendStatusUpdate")
                    {
                        _oauth.Token = proxyArgs.txtAccessToken;
                        _oauth.TokenSecret = proxyArgs.txtAccessTokenSecret;
                        _oauth.Verifier = proxyArgs.txtoAuth_verifier;

                        string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                        xml += "<current-status>" + proxyArgs.txtProfileStatus + "</current-status>";

                        string response = _oauth.APIWebRequest("PUT", "http://api.linkedin.com/v1/people/~/current-status", xml);
                        if (response == "")
                            proxyArgs.txtApiResponse = "Your new status updated in LinkedIn";

                        return proxyArgs;
                    }
                    else if (proxyArgs.txtCommandName == "RequestProfile")
                    {
                        _oauth.Token = proxyArgs.txtAccessToken;
                        _oauth.TokenSecret = proxyArgs.txtAccessTokenSecret;
                        _oauth.Verifier = proxyArgs.txtoAuth_verifier;

                        // http://developer.linkedin.com/docs/DOC-1061 - This page describes all available Fields from LinkedIn
                        string response = _oauth.APIWebRequest("GET", "https://api.linkedin.com/v1/people/~:(first-name,last-name,headline,current-status,connections,summary)", null);
                        proxyArgs.txtApiResponse = response;

                        return proxyArgs;
                    }
                    else return null;
                }
                catch (Exception x)
                {
                    proxyArgs = new SPLinkedInProxyArgs();
                    proxyArgs.txtErrorMsg = x.Message;
                    return proxyArgs;
                }
            }
            else return null;
        }
    }
}
